var cusomerList;
$(function() {
		var dataColums = [ [ {
			field : 'id',
			title : 'matchId',
			hidden : true
		}, {
			field : 'createdAt',
			title : '时间',
			width : 90,
			align : 'center',
			formatter : function(v) {
				return utils.format(v, 'yyyy-MM-dd');
			}
		}, {
			field : 'billNo',
			title : '单号',
			width : 200,
			align : 'center'
		},
		/* {field:'team',title:'队伍',width:130,align:'center',formatter:function(v,data,index){
		  return '<a onclick="loadMatchDetail('+data.matchId+')">'+data.hostTeam+'-'+data.guestTeam+'</a>';
		}},
		{field:'score',title:'比分',width:60,align:'center',formatter:function(v,data,index){
		  var color = 'red';
		  if(data.matchResult == 1){
			  color = 'yellow';
		  }else if(data.matchResult < 1){
			  color = 'green';
		  }
		  return '<span style="color:'+color+'">'+data.socre_h+'-'+data.socre_g+'</span>';
		}}, */
		{
			field : 'totalAmount',
			title : '金额',
			width : 150,
			align : 'center'
		}, {
			field : 'company',
			title : '单位',
			width : 200,
			align : 'center'
		},{
			field : 'mobile',
			title : '联系方式',
			width : 150,
			align : 'center'
		},{
			field : 'receiver',
			title : '收货人',
			width : 150,
			align : 'center'
		},{
			field : 'option',
			title : '操作',
			width : 200,
			align : 'center',formatter:function(v,data,index){
				  return '<a onclick="loadDetail('+data.id+')">查看</a>';
			}
		}] ];
		
		$("#goodsData").datagrid({
			columns : dataColums
		});
		var now = new Date();
		$('.paramDate[name=startTime]').val(now.getFullYear()+"-01"+"-01");
		$('.paramDate[name=endTime]').val(now.getFullYear()+"-12"+"-31");
		$('.paramDate').datebox({
			formatter:function(date){
				var y = date.getFullYear();
				var m = date.getMonth()+1;
				m = m>9?m:('0'+m);
				var d = date.getDate()>9?date.getDate():('0'+date.getDate());
				return y+'-'+m+'-'+d;
			},
			parser:function(s){
				var t = Date.parse(s);
				if (!isNaN(t)){
					return new Date(t);
				} else {
					return new Date();
				}
			}
		});
		
		
		var customerColums = [ [ {
			field : 'id',
			title : 'customerId',
			hidden : true
		}, {
			field : 'receiver',
			title : '收货人',
			width : 90,
			align : 'center'
		}, {
			field : 'company',
			title : '单位',
			width : 120,
			align : 'center'
		}, {
			field : 'mobile',
			title : '联系方式',
			width : 90,
			align : 'center'
		}, {
			field : 'address',
			title : '地址',
			width : 233,
			align : 'center'
		}]];
		$('#build').click(function(){
			var queryData = createReqData();
			if(queryData.receiver == ''){
				$.messager.alert('提示','收货人必填','Warning');
				return;
			}
			utils.postJson({url:'/queryCompanyList',data:queryData,success:function(response){
				var list = response.result;
				if(list.length > 1){
					$("#cusomerListDialog").dialog({
					    		title: '客户选择',
					    	    width: 550,
					    	    height: 180,
					    	    closed: false,
					    	    cache: false,
					    	    modal: true
					});
					if(cusomerList == null){
						cusomerList = $("#cusomerList").datagrid({
							columns : customerColums,
							onDblClickRow:function(rowIndex, rowData){
								queryData.customerId = rowData.id;
								$.each(queryData,function(key,value){
									if(rowData[key]){
										queryData[key] = rowData[key]; 
									}
								});
								utils.postJson({url:'/buildBillStatement',data:queryData,success:function(response){
									//导出
									if(response.code==0){
										$.messager.alert('提示','<a href="'+response.result+'">下载</a>','Warning');
									}else{
										$.messager.alert('提示',response.result,'Warning');
									}
									$("#cusomerListDialog").dialog('close');
								}});
							}
						});
					}
					cusomerList.datagrid('loadData',{rows:list});
					return;
				}else if(list.length == 1){
					queryData.customerId = list[0].id;
				}else{
					$.messager.alert('提示','客户还未录入','Warning');
					return;
				}
				
				utils.postJson({url:'/buildBillStatement',data:queryData,success:function(response){
					//导出
					if(response.code==0){
						$.messager.alert('提示','<a href="'+response.result+'">下载</a>','Warning');
					}else{
						$.messager.alert('提示',response.result,'Warning');
					}
				}});
			}});
		});
	});
	
	function loadDetail(billId){
		window.parent.addTabs('/print.html?billId='+billId,"订单详情");
	}
	function query(){
		var queryData = createReqData();
		if(queryData.receiver == ''){
			$.messager.alert('提示','收货人必填','Warning');
			return;
		}
		utils.postJson({url:'/queryBill',data:queryData,success:function(response){
			console.info(response.result);
			$('#totalMoney').text(response.result.totalMoney);
			$('#goodsData').datagrid('loadData',{
				rows:response.result.list,
				total:response.result.list.length
			});
		}});
	}
	function createReqData(){
		var queryData = {};
		$('#form').find('input').each(function(i,input){
			if(input.name != ''){
				queryData[input.name] = input.value; 
			}
		});
		if(queryData.endTime != ''){
			queryData.endTime += ' 23:59:59';
		}
		return queryData;
	}